package autoteam.tables;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import autoteam.League;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class CoachTableModel extends AutoTeamTableModel
{
    private static final long serialVersionUID = 7552079379294919886L;

    private League currentLeague;
    private int rowCount;
    private Map<Integer,String> colMap;
    List<DBRecord> coachRecords;
    
    public static String[] columnNames = 
    { "CoachID", "Team", "First Name", "Last Name" };

	private DataAccessObject dao;
    

    public CoachTableModel( )
    {
    	super( );
    }

    public CoachTableModel( League newLeague )
    {
		this.currentLeague = newLeague;
		dao = new DataAccessObject();
		coachRecords = dao.getAllRecords("coaches");
		rowCount = coachRecords.size();
		colMap = new HashMap<Integer,String>();
		colMap.put(0, "id");
		colMap.put(1, "teamId");
		colMap.put(2, "firstname");
		colMap.put(3, "lastname");
    }
   
    public League getCurrentLeague()
    {
    	return currentLeague;
    }
    
    public void setCurrentLeague( League newLeague )
    {
    	currentLeague = newLeague;
    }
    
    @ Override
    public void add( int j )
    {
    	++rowCount;
    	fireTableDataChanged();
    }

    @ Override
    public void delete( int r )
    {
    	ColumnData coachToDelete = new ColumnData("id",this.getValueAt(r,0));
    	dao.deleteRecord("coaches", coachToDelete);
    	rowCount--;
    	fireTableDataChanged();
    }

    @ Override
    public int getColumnCount( )
    {
		return 4;
    }

    @ Override
    public int getRowCount( )
    {
		return rowCount;
    }

    public String getColumnName(int col)
    {
    	return columnNames[col];
    }
    
    @ Override
    public Object getValueAt( int rowIndex, int colIndex )
    {
    	String colName = colMap.get(colIndex);
    	coachRecords = dao.getAllRecords("coaches");
    	if (colIndex == 1)
    	{
    		int i = (Integer) coachRecords.get(rowIndex).getFieldValue(colName);
    		ColumnData col = new ColumnData("id", i);
    		DBRecord rec = dao.getRecord("teams", col);
    		return rec.getFieldValue("teamName");
    	}
    	return coachRecords.get(rowIndex).getFieldValue(colName);
    }   

}
